package com.situ.util.controller;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.login.entity.SysUser;
import com.situ.util.entity.*;
import com.situ.util.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import com.situ.util.service.CstCustomerService;
import com.situ.util.*;

import javax.validation.constraints.Size;
import java.beans.PropertyEditor;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * CstCustomer的路由接口服务
 * 
 * @author 
 *
 */
@RestController
public class CstCustomerController {

	/** CstCustomerService服务 */
	@Autowired
	private CstCustomerService cstCustomerService;
	@Autowired
	private OrdersService ordersService;

	//带条件的分页查询客户表
	@RequestMapping("/queryCustByLike")
	public ResponseData queryByLike(CstCustomer customer,
									@RequestParam(defaultValue ="1",required = false) int startPage ,
									@RequestParam(defaultValue = "5",required = false) int PageSize){
		try {
			PageHelper.startPage(startPage, PageSize);
			List<Map> clist=cstCustomerService.queryallCustomer(customer);//查询的上面和下面加一行代码包起来
			PageInfo pageInfo = new PageInfo<Map>(clist,5);
			return ResponseData.ok().putDataValue("customerlist",clist).putDataValue("pageInfo",pageInfo).putDataValue("Pagesize", PageSize);
		}catch (Exception e){
			e.printStackTrace();
			return ResponseData.serverInternalError();
		}
	}

	/**
	 * 通过id查询CstCustomer数据的方法
	 * @param id
	 * @return
	 */
	@GetMapping("/queryCstCustomerById")
	public ResponseData findOne(String id) {
		return ResponseData.ok().putDataValue("customer",cstCustomerService.showCustById(id));
	}

	//查数据字典的下拉列表和客户经理的下拉列表
	@GetMapping("/querydict")
	public ResponseData finddict() {
		List<BasDict> plist=cstCustomerService.querydict("地区");
		List<BasDict> llist=cstCustomerService.querydict("企业客户等级");
		List<SysUser> ulist=cstCustomerService.queryusers();
		return ResponseData.ok()
				.putDataValue("place",plist)
				.putDataValue("custlevel",llist)
				.putDataValue("ulist", ulist);
	}
//查客户等级的id
	@GetMapping("/findid")
	public ResponseData findlevelid(String levelname) {
		return ResponseData.ok().putDataValue("levelid", cstCustomerService.querylevelid(levelname));
	}

	//查客户经理id
	@GetMapping("/finduserid")
	public ResponseData finduserid(String username) {
		return ResponseData.ok().putDataValue("userid", cstCustomerService.queryuseridbyname(username));
	}

	//分页查询客户联系人表
	@RequestMapping("/queryLinkmanById")
	public ResponseData queryLinkmanById(String id,
									@RequestParam(defaultValue ="1",required = false) int startPage ,
									@RequestParam(defaultValue = "5",required = false) int pageSize){
		try {
			PageHelper.startPage(startPage, pageSize);
			List<CstLinkman> clist=cstCustomerService.queryLinkmanById(id);//查询的上面和下面加一行代码包起来
			PageInfo pageInfo = new PageInfo<CstLinkman>(clist,5);
			return ResponseData.ok().putDataValue("linkmanlist",clist).putDataValue("pageInfo",pageInfo).putDataValue("Pagesize", pageSize);
		}catch (Exception e){
			e.printStackTrace();
			return ResponseData.serverInternalError();
		}
	}
    //分页查询客户交往记录表
    @RequestMapping("/queryatvById")
    public  ResponseData queryatvById(String id,
                                         @RequestParam(defaultValue ="1",required = false) int startPage ,
                                         @RequestParam(defaultValue = "5",required = false) int pageSize){
        try {
            PageHelper.startPage(startPage, pageSize);
            List<CstActivity> clist=cstCustomerService.queryatvById(id);//查询的上面和下面加一行代码包起来
            PageInfo pageInfo = new PageInfo<CstActivity>(clist,5);
            return ResponseData.ok().putDataValue("atvlist",clist).putDataValue("pageInfo",pageInfo).putDataValue("Pagesize", pageSize);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseData.serverInternalError();
        }
    }
    //分页查询客户订单表
    @RequestMapping("/queryorder")
    public  ResponseData queryorder(String id,
                                      @RequestParam(defaultValue ="1",required = false) int startPage ,
                                      @RequestParam(defaultValue = "5",required = false) int pageSize){
		try {
            PageHelper.startPage(startPage, pageSize);
            List<Orders> clist=cstCustomerService.queryorders(id);//查询的上面和下面加一行代码包起来
            PageInfo pageInfo = new PageInfo<Orders>(clist,5);
            return ResponseData.ok().putDataValue("orderslist",clist).putDataValue("pageInfo",pageInfo).putDataValue("Pagesize", pageSize);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseData.serverInternalError();
        }
    }
	//查客户等级的id
	@GetMapping("/findatvbyid")
	public ResponseData findatvbyid(Integer atvid) {
		CstActivity c=cstCustomerService.queryatvByaId(atvid);
		return ResponseData.ok().putDataValue("actv",c);
	}

	//查询order表
	//查询订单链接与产品表
	@GetMapping("/queryorderbyid")
	public ResponseData queryorderbyid(Integer oid) {
		Map c=cstCustomerService.queryorderbyid(oid);
		List<Map> olist=cstCustomerService.queryPLByid(oid);
		return ResponseData.ok().putDataValue("oders",c).putDataValue("orderlist",olist );
	}

    //添加客户联系人表
    @GetMapping("/addLinkman")
    public ResponseData addLinkman(CstLinkman c) {
        cstCustomerService.addlinkman(c);
        return ResponseData.ok();
    }

    //添加客户交往记录表

    @GetMapping("/addatv")
    public ResponseData addatv(CstActivity c) {
        cstCustomerService.addatv(c);
        return ResponseData.ok();
    }

	//修改客户表
	@GetMapping("/updateCustById")
	public ResponseData updateCustomerById(CstCustomer c) {
		cstCustomerService.updateCustomerById(c);
		return ResponseData.ok();
	}

    //修改客户联系人表
    @GetMapping("/updateLinkmanById")
    public ResponseData updateLinkmanById(CstLinkman c) {
        cstCustomerService.updatelinkman(c);
        return ResponseData.ok();
    }

    //修改客户交往记录表
    @GetMapping("/updateatvbyid")
    public ResponseData updateatvbyid(CstActivity c) {
		System.out.print("进来了");
        cstCustomerService.updateatv(c);
        return ResponseData.ok();
    }

	//删除客户表
	@GetMapping("/delcust")
	public ResponseData delcust(String id) {
		cstCustomerService.delcustomer(id);
		return ResponseData.ok();
	}
    //删除客户联系人表
    @GetMapping("/delLinkmanById")
    public ResponseData delLinkmanById(Integer id) {
        cstCustomerService.dellinkman(id);
        return ResponseData.ok();
    }

    //删除客户交往记录表
    @GetMapping("/delatvbyid")
    public ResponseData delatvbyid(Integer id) {
        cstCustomerService.delatv(id);
        return ResponseData.ok();
    }

	//时间转换器
	@InitBinder  //
	public void dateconvert(WebDataBinder bind){
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		PropertyEditor n=new CustomDateEditor(format, true);
		bind.registerCustomEditor(Date.class, n);
	}


}
